//
//  BehindArticleViewController.m
//  Movie
//
//  Created by cjy on 16/3/26.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "BehindArticleViewController.h"
#import "BehindArticleCell.h"
#import "NetworkHandeler.h"
#import "ModelBehindArticle.h"
#import "PushBehindArticleViewController.h"
#import <MJRefresh.h>
#import "UIScrollView+JFRefresh.h"


@interface BehindArticleViewController ()<UITableViewDataSource,UITableViewDelegate,NetworkHandelerDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *array;

@property (nonatomic, retain) NSString *str;
@property (nonatomic, assign) NSInteger pag;

@end

@implementation BehindArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target: self action:@selector(presentLeftMenuViewController:)];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"幕后";
    
    [self createtableView];

    self.array = [NSMutableArray array];
    self.str = [[NSString alloc] init];
    
    self.str = @"http://service.vmovier.com/api/backstage/getPostByCate?cateid=2&p=1";
    self.pag = 1;
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithAction:^{
        [weakSelf headerActionOpration];
    }customControl:^(JFDrawTextView *drawView) {
        drawView.refreshText = @"JuJu-vMoive";
        drawView.textColor = [UIColor colorWithRed:0.641 green:1.000 blue:1.000 alpha:1.000];
    }];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.str = [NSString stringWithFormat:@"http://service.vmovier.com/api/backstage/getPostByCate?cateid=2&p=1"];
            self.pag = 1;
//            NSLog(@"刷新完毕");
            
            [self.tableView.mj_header endRefreshing];
        });
    }];
    
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    //上拉加载
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.str = [NSString stringWithFormat:@"http://service.vmovier.com/api/backstage/getPostByCate?cateid=2&p=%ld",(long)++self.pag];
            
            [self handle:self.str];
            [self.tableView reloadData];
            
        });
    }];
    
    [self handle:self.str];
}


- (void)headerActionOpration{
    [self.tableView performSelector:@selector(endHeaderRefresh) withObject:nil afterDelay:4];
}


- (void)handle:(NSString *)str{

    self.str = str;
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
    
}

- (void)handelerDidComplete:(id)result{

    
    NSArray *arr = [result objectForKey:@"data"];

    for (NSDictionary *dic in arr) {
        
        ModelBehindArticle *model = [[ModelBehindArticle alloc]init];
        model.imageV = [dic objectForKey:@"image"];
        model.title = [dic objectForKey:@"title"];
        model.share_link = [dic objectForKey:@"share_link"];
        
        [self.array addObject:model];
        
    }
    
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
}

- (void)createtableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    [self.tableView registerClass:[BehindArticleCell class] forCellReuseIdentifier:@"pool"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    BehindArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.backgroundColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    
    ModelBehindArticle *model = self.array[indexPath.row];
    
    
    [cell passModel:model];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    PushBehindArticleViewController *push = [[PushBehindArticleViewController alloc]init];

    ModelBehindArticle *model = self.array[indexPath.row];
    
    [push passModel:model];
    [self.navigationController pushViewController:push animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
